package com.leo.utils;

import android.util.Log;

/**
 *
 * Created by leo.
 */
public class L {

    public static boolean WRITE_LOG = false;

    private static final String TAG = "LLOG";

    public static void enbale(){
        WRITE_LOG = true;
    }

    public static void i() {
        log(Log.INFO, null, "...");
    }

    public static void d() {
        log(Log.DEBUG, null, "...");
    }

    public static void e() {
        log(Log.ERROR, null, "...");
    }

    public static void v() {
        log(Log.VERBOSE, null, "...");
    }

    public static void w() {
        log(Log.WARN, null, "...");
    }

    public static void v(Throwable t) {
        log(Log.VERBOSE, t, null);
    }

    public static void v(Object s1) {
        log(Log.VERBOSE, null, s1);
    }

    public static void v(String s1, Object... args) {
        log(Log.VERBOSE, null, s1, args);
    }

    public static void v(Throwable t, Object s1) {
        log(Log.VERBOSE, t, s1);
    }

    public static void v(Throwable t, String s1, Object... args) {
        log(Log.VERBOSE, t, s1, args);
    }

    public static void d(Throwable t) {
        log(Log.DEBUG, t, null);
    }

    public static void d(Object s1) {
        log(Log.DEBUG, null, s1);
    }

    public static void d(String s1, Object... args) {
        log(Log.DEBUG, null, s1, args);
    }

    public static void d(Throwable t, Object s1) {
        log(Log.DEBUG, t, s1);
    }

    public static void d(Throwable t, String s1, Object... args) {
        log(Log.DEBUG, t, s1, args);
    }

    public static void i(Throwable t) {
        log(Log.INFO, t, null);
    }

    public static void i(Object s1) {
        log(Log.INFO, null, s1);
    }

    public static void i(String s1, Object... args) {
        log(Log.INFO, null, s1, args);
    }

    public static void i(Throwable t, Object s1) {
        log(Log.INFO, t, s1);
    }

    public static void i(Throwable t, String s1, Object... args) {
        log(Log.INFO, t, s1, args);
    }

    public static void w(Throwable t) {
        log(Log.WARN, t, null);
    }

    public static void w(Object s1) {
        log(Log.WARN, null, s1);
    }

    public static void w(String s1, Object... args) {
        log(Log.WARN, null, s1, args);
    }

    public static void w(Throwable t, Object s1) {
        log(Log.WARN, t, s1);
    }

    public static void w(Throwable t, String s1, Object... args) {
        log(Log.WARN, t, s1, args);
    }

    public static void e(Throwable t) {
        log(Log.ERROR, t, null);
    }

    public static void e(Object s1) {
        log(Log.ERROR, null, s1);
    }

    public static void e(String s1, Object... args) {
        log(Log.ERROR, null, s1, args);
    }

    public static void e(Throwable t, Object s1) {
        log(Log.ERROR, t, s1);
    }

    public static void e(Throwable t, String s1, Object... args) {
        log(Log.ERROR, t, s1, args);
    }

    private static void log(final int pType, final Throwable t,final String s1, final Object... args) {
        final String message = (args == null || args.length ==0) ? s1: String.format(s1, args);
        log(pType,t,message);
    }

    private static void log(final int pType, final Throwable t,final Object s1) {
        if (pType == Log.ERROR || WRITE_LOG) {
            final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4];

            final String fullClassName = stackTraceElement.getClassName();
            final String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
            final int lineNumber = stackTraceElement.getLineNumber();
            final String method = stackTraceElement.getMethodName();

            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("(");
            stringBuilder.append(lineNumber);
            stringBuilder.append(")");
            stringBuilder.append(className);
            stringBuilder.append(".");
            stringBuilder.append(method);
            stringBuilder.append("(): ");

            if (s1 != null) {
                stringBuilder.append(s1.toString());
            }

            switch (pType) {
                case Log.VERBOSE:
                    if (t != null) {
                        Log.v(TAG, stringBuilder.toString(), t);
                    } else {
                        Log.v(TAG, stringBuilder.toString());
                    }
                    break;

                case Log.DEBUG:
                    if (t != null) {
                        Log.d(TAG, stringBuilder.toString(), t);
                    } else {
                        Log.d(TAG, stringBuilder.toString());
                    }
                    break;

                case Log.INFO:
                    if (t != null) {
                        Log.i(TAG, stringBuilder.toString(), t);
                    } else {
                        Log.i(TAG, stringBuilder.toString());
                    }
                    break;

                case Log.WARN:
                    if (t != null) {
                        Log.w(TAG, stringBuilder.toString(), t);
                    } else {
                        Log.w(TAG, stringBuilder.toString());
                    }
                    break;

                case Log.ERROR:
                    if (t != null) {
                        Log.e(TAG, stringBuilder.toString(), t);
                    } else {
                        Log.e(TAG, stringBuilder.toString());
                    }
                    break;
            }
        }
    }

    public static boolean isEnable() {
        return WRITE_LOG;
    }
}
